import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_color_plugin/flutter_color_plugin.dart';
import 'package:detect/utils/global.dart';
import 'package:detect/utils/modal_util.dart';

class DatePicker extends StatefulWidget {
  final bool disabled;
  final CupertinoDatePickerMode mode;
  final DateTime? value;
  final void Function(DateTime result)? onChange;
  final Widget Function(BuildContext context, DateTime? result) builder;

  const DatePicker({
    super.key,
    this.disabled = false,
    this.mode = CupertinoDatePickerMode.dateAndTime,
    this.value,
    this.onChange,
    required this.builder,
  });

  @override
  State<DatePicker> createState() => _DatePickerState();
}

class _DatePickerState extends State<DatePicker> {
  DateTime? _result;

  DateTime? get result => _result ??= widget.value;

  set result(DateTime? value) => _result = value;

  @override
  Widget build(BuildContext context) => InkWell(
        onTap: widget.disabled
            ? null
            : () {
                DateTime now = DateTime.now();
                result ??= now;
                ModalUtil.dialog<DateTime>(
                  context: context,
                  width: Global.ds(400),
                  height: Global.ds(300),
                  backgroundColor: ColorUtil.color("#1A3959"),
                  borderRadius: BorderRadius.circular(Global.ds(16)),
                  child: Padding(
                    padding: EdgeInsets.all(Global.ds(15)),
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      crossAxisAlignment: CrossAxisAlignment.stretch,
                      children: [
                        Center(
                          child: Text(
                            widget.mode == CupertinoDatePickerMode.date
                                ? "选择日期"
                                : widget.mode == CupertinoDatePickerMode.time
                                    ? "选择时间"
                                    : "选择日期时间",
                            style: TextStyle(
                              fontSize: Global.ds(24),
                              color: Colors.white,
                              decoration: TextDecoration.none,
                            ),
                          ),
                        ),
                        Divider(height: Global.ds(10), color: Colors.transparent),
                        Expanded(
                          child: CupertinoTheme(
                            data: CupertinoThemeData(textTheme: CupertinoTextThemeData(dateTimePickerTextStyle: TextStyle(fontSize: Global.ds(18), color: Colors.white))),
                            child: CupertinoDatePicker(
                              mode: widget.mode,
                              use24hFormat: true,
                              initialDateTime: result,
                              maximumDate: now,
                              dateOrder: DatePickerDateOrder.ymd,
                              onDateTimeChanged: (dateTime) => result = dateTime,
                            ),
                          ),
                        ),
                        Divider(height: Global.ds(10), color: Colors.transparent),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: [
                            TextButton(
                              onPressed: () => Navigator.pop<DateTime>(context),
                              style: TextButton.styleFrom(
                                textStyle: TextStyle(fontSize: Global.ds(18)),
                                fixedSize: Size(Global.ds(120), Global.ds(40)),
                                foregroundColor: ColorUtil.color("#687079"),
                                backgroundColor: ColorUtil.color("#1A263D"),
                              ),
                              child: const Text("取消"),
                            ),
                            TextButton(
                              onPressed: () => Navigator.pop<DateTime>(context, result),
                              style: TextButton.styleFrom(
                                textStyle: TextStyle(fontSize: Global.ds(18)),
                                fixedSize: Size(Global.ds(120), Global.ds(40)),
                                foregroundColor: Colors.white,
                                backgroundColor: ColorUtil.color("#2CA6E0"),
                              ),
                              child: const Text("确定"),
                            ),
                          ],
                        ),
                      ],
                    ),
                  ),
                ).then((result) {
                  if (result != null) {
                    setState(() => this.result = result.copyWith(second: 0));
                    widget.onChange?.call(this.result!);
                  }
                });
              },
        child: widget.builder(context, result),
      );
}
